package cn.chiship.framework.business.biz.business.service.impl;

import cn.chiship.framework.upms.biz.system.entity.UpmsCategoryDict;
import cn.chiship.framework.upms.biz.system.entity.UpmsCategoryDictExample;
import cn.chiship.framework.upms.biz.system.mapper.UpmsCategoryDictMapper;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import cn.chiship.framework.business.biz.business.mapper.BusinessFormMapper;
import cn.chiship.framework.business.biz.business.entity.BusinessForm;
import cn.chiship.framework.business.biz.business.entity.BusinessFormExample;
import cn.chiship.framework.business.biz.business.service.BusinessFormService;
import cn.chiship.sdk.framework.pojo.vo.TreeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义表单业务接口实现层
 * 2022/8/18
 *
 * @author lijian
 */
@Service
public class BusinessFormServiceImpl extends BaseServiceImpl<BusinessForm, BusinessFormExample> implements BusinessFormService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessFormServiceImpl.class);

    @Resource
    BusinessFormMapper businessFormMapper;

    @Resource
    UpmsCategoryDictMapper upmsCategoryDictMapper;

    @Override
    public BaseResult selectDetailsByPrimaryKey(Object id) {
        BusinessFormExample formExample = new BusinessFormExample();
        formExample.createCriteria().andIdEqualTo(id.toString());
        List<BusinessForm> businessForms = businessFormMapper.selectByExampleWithBLOBs(formExample);
        if (businessForms.isEmpty()) {
            return BaseResult.ok();
        }
        return BaseResult.ok(businessForms.get(0));
    }

    @Override
    public BaseResult saveFormContent(String id, String fromContent, String modifyBy) {
        BusinessForm businessForm = businessFormMapper.selectByPrimaryKey(id);
        businessForm.setGmtModified(System.currentTimeMillis());
        businessForm.setFormContent(fromContent);
        businessForm.setModifyBy(modifyBy);
        businessForm.setStatus(Byte.valueOf("0"));
        businessFormMapper.updateByPrimaryKeyWithBLOBs(businessForm);
        return BaseResult.ok();
    }

    @Override
    public BaseResult listTree() {
        BusinessFormExample businessFormExample = new BusinessFormExample();
        List<BusinessForm> businessForms = businessFormMapper.selectByExample(businessFormExample);
        UpmsCategoryDictExample upmsCategoryDictExample = new UpmsCategoryDictExample();
        UpmsCategoryDictExample.Criteria upmsCategoryDictCriteria = upmsCategoryDictExample.createCriteria();
        upmsCategoryDictCriteria.andIsDeletedEqualTo(BaseConstants.NO).andTypeEqualTo("05");
        upmsCategoryDictExample.setOrderByClause("orders desc");
        List<UpmsCategoryDict> upmsCategoryDicts = upmsCategoryDictMapper.selectByExample(upmsCategoryDictExample);
        return BaseResult.ok(assemblyCategoryDictTree("0", upmsCategoryDicts, businessForms));
    }

    private List<TreeVo> assemblyCategoryDictTree(String pid, List<UpmsCategoryDict> upmsCategoryDicts, List<BusinessForm> businessForms) {
        List<TreeVo> treeVos = new ArrayList<>();
        for (UpmsCategoryDict categoryDict : upmsCategoryDicts) {
            if (pid.equals(categoryDict.getPid())) {
                TreeVo treeVo = new TreeVo();
                treeVo.setId(categoryDict.getId());
                treeVo.setLabel(categoryDict.getName());
                treeVo.setLeaf(false);
                treeVo.setExt("1");
                treeVo.setChildren(listTreeByCategory(categoryDict.getId(), businessForms));
                treeVos.add(treeVo);
            }
        }
        for (int i = 0; i < treeVos.size(); i++) {
            List<TreeVo> children = assemblyCategoryDictTree(treeVos.get(i).getId(), upmsCategoryDicts, businessForms);
            if (!children.isEmpty()) {
                treeVos.get(i).setChildren(children);
            }
        }
        return treeVos;
    }

    private List<TreeVo> listTreeByCategory(String categoryId, List<BusinessForm> businessForms) {
        List<TreeVo> treeVos = new ArrayList<>();
        for (BusinessForm businessForm : businessForms) {
            if (businessForm.getCategoryId().equals(categoryId)) {
                TreeVo treeVo = new TreeVo();
                treeVo.setId(businessForm.getId());
                treeVo.setLabel(businessForm.getName());
                treeVo.setLeaf(true);
                treeVo.setExt("2");
                treeVo.setIcon(businessForm.getIcon());
                treeVos.add(treeVo);
            }
        }
        return treeVos;
    }
}
